package com.ruoyi.note.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.note.domain.TBook;
import com.ruoyi.note.domain.TBookContent;
import com.ruoyi.note.mapper.TBookContentMapper;
import com.ruoyi.note.mapper.TBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.note.mapper.TBookChapterMapper;
import com.ruoyi.note.domain.TBookChapter;
import com.ruoyi.note.service.ITBookChapterService;

/**
 * 章节管理Service业务层处理
 *
 * @author cl
 * @date 2021-04-15
 */
@Service
public class TBookChapterServiceImpl implements ITBookChapterService {
    @Autowired
    private TBookChapterMapper tBookChapterMapper;

    @Autowired
    private TBookContentMapper tBookContentMapper;

    @Autowired
    private TBookMapper tBookMapper;

    /**
     * 查询章节管理
     *
     * @param id 章节管理ID
     * @return 章节管理
     */
    @Override
    public TBookChapter selectTBookChapterById(Long id) {
        return tBookChapterMapper.selectTBookChapterById(id);
    }

    @Override
    public int countAll(Long userId) {
        return tBookChapterMapper.countAll(userId);
    }

    /**
     * 查询章节管理列表
     *
     * @param tBookChapter 章节管理
     * @return 章节管理
     */
    @Override
    public List<TBookChapter> selectTBookChapterList(TBookChapter tBookChapter) {
        return tBookChapterMapper.selectTBookChapterList(tBookChapter);
    }

    @Override
    public List<String> update() {
        List<TBook> books = tBookMapper.selectTBookList(new TBook());
        List<TBookChapter> chapters = tBookChapterMapper.selectTBookChapterList(new TBookChapter());
        List<TBookContent> contents = tBookContentMapper.selectTBookContentList(new TBookContent());
        List<String> list = new ArrayList<>();
        Map<Long, Integer> chpaterIdMap = new HashMap<>();
        for (int i = 0; i < books.size(); i++) {
            TBook book = books.get(i);
            list.add("update t_book set id = " + (i + 1) + " where id = " + book.getId());
            for (int i1 = 0; i1 < chapters.size(); i1++) {
                TBookChapter chapter = chapters.get(i1);
                if (chapter.getBookId().toString().equals(book.getId().toString())) {
                    list.add("update t_book_chapter set id = " + (i1 + 1) + ",book_id = " + (i + 1) + " where id = " + chapter.getId());
                    chpaterIdMap.put(chapter.getId(), i1 + 1);
                    chapter.setId((long) (i1 + 1));
                    for (int i2 = 0; i2 < contents.size(); i2++) {
                        TBookContent content = contents.get(i2);
                        if (content.getChapterId().toString().equals(chapter.getId().toString())) {
                            list.add("update t_book_content set id = " + (i2 + 1) + ",book_id = " + (i + 1) + ",chapter_id = " + (i1 + 1) + " where id = " + content.getId());
                        }
                    }
                }
            }
        }
        for (int i1 = 0; i1 < chapters.size(); i1++) {
            TBookChapter chapter = chapters.get(i1);
            if (chapter.getParentId() != null) {
                int newId = chpaterIdMap.get(chapter.getParentId());
                list.add("update t_book_chapter set parent_id = " + newId + " where id = " + chapter.getId());

            }
        }
        return list;
    }

    /**
     * 新增章节管理
     *
     * @param tBookChapter 章节管理
     * @return 结果
     */
    @Override
    public int insertTBookChapter(TBookChapter tBookChapter) {
        tBookChapter.setCreateTime(DateUtils.getNowDate());
        tBookMapper.updateTBookTime(tBookChapter.getBookId());
        return tBookChapterMapper.insertTBookChapter(tBookChapter);
    }

    /**
     * 修改章节管理
     *
     * @param tBookChapter 章节管理
     * @return 结果
     */
    @Override
    public int updateTBookChapter(TBookChapter tBookChapter) {
        tBookMapper.updateTBookTime(tBookChapter.getBookId());
        return tBookChapterMapper.updateTBookChapter(tBookChapter);
    }

    /**
     * 批量删除章节管理
     *
     * @param ids 需要删除的章节管理ID
     * @return 结果
     */
    @Override
    public int deleteTBookChapterByIds(Long[] ids) {
        return tBookChapterMapper.deleteTBookChapterByIds(ids);
    }

    /**
     * 删除章节管理信息
     *
     * @param id 章节管理ID
     * @return 结果
     */
    @Override
    public int deleteTBookChapterById(Long id) {
        return tBookChapterMapper.deleteTBookChapterById(id);
    }
}
